package com.example.myapplication.tools;


import android.os.Build;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.speech.tts.UtteranceProgressListener;
import android.util.Log;

import com.example.myapplication.WordApp;
import com.example.myapplication.events.TTSWorkingEvent;
import com.example.myapplication.model.Word;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;

import androidx.annotation.RequiresApi;

public class TTSHelper {

    private static TTSHelper mInstance;
    private TextToSpeech mTTS;
    private boolean isReady = false;

    private float speed = 1.0f;

    private TTSHelper()
    {
        mTTS = new TextToSpeech(WordApp.AppCtx,
                new TextToSpeech.OnInitListener() {
                    @Override
                    public void onInit(int status) {
                        isReady = status == TextToSpeech.SUCCESS;
                        if(isReady) {
                            mTTS.setLanguage(Locale.ENGLISH);
                            mTTS.setSpeechRate(1.0f);
                            speed = 1.0f;
                        }
                    }
                });

        mTTS.setOnUtteranceProgressListener(new UtteranceProgressListener() {
            @Override
            public void onStart(String s) {

            }

            @Override
            public void onDone(String s) {
                //TODO 通知UI
                EventBus.getDefault().post(  new TTSWorkingEvent(s) );
            }

            @Override
            public void onError(String s) {

            }
        });

    }

    public static TTSHelper getInstance() {
        if(mInstance == null){
            mInstance = new TTSHelper();
        }
        return mInstance;
    }

    public void speak(String txt) {
            if (isReady) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
                    mTTS.speak(txt, TextToSpeech.QUEUE_FLUSH, new Bundle(), "");
                else
                    mTTS.speak(txt, TextToSpeech.QUEUE_FLUSH, new HashMap<>());
            }
    }

    public boolean toFile(String txt) {
        int res = -1;
        if(isReady) {
            String path = WordApp.AppCtx.getExternalFilesDir("").getAbsolutePath();
            path += "/voice.wav";
            File file = new File(path);
            file.delete();
            try {
                if(file.createNewFile()) {
                    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                        res = mTTS.synthesizeToFile(txt, new Bundle(), file, "File");
                    } else {
                        res = mTTS.synthesizeToFile(txt, new HashMap<>(), file.getAbsolutePath());
                    }
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return res == 0;
    }

    public void setSpeed(float d) {
        if(isReady) {
            speed += d;
            speed = Math.min(2.0f,speed);
            speed = Math.max(0.1f,speed);
            mTTS.setSpeechRate(speed);
        }
    }

    public String getSpeed() {
        return String.format("%.1f", speed);
    }
}
